<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="500px" @confirm="handleSubmit"
            @close="handleClose" style="max-height: 60vh">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="账号" prop="account">
                    <el-input v-model="formData.account" clearable placeholder="请填写账号" />
                </el-form-item>
                <el-form-item label="密码" :prop="popupTitle === '新增用户表' ? 'password' : ''">
                    <el-input v-model="formData.password" clearable placeholder="请填写密码" />
                </el-form-item>
                <el-form-item label="角色" :prop="popupTitle === '新增用户表' ? 'role_id' : ''">
                    <el-select class="flex-1" v-model="formData.role_id" clearable placeholder="请选择角色">
                        <el-option v-for="(item, index) in roles" :key="index" :label="item.role_name"
                            :value="parseInt(item.id)" />
                    </el-select>
                </el-form-item>
                <el-form-item label="昵称" :prop="popupTitle === '新增用户表' ? 'nickname' : ''">
                    <el-input v-model="formData.nickname" clearable placeholder="请填写昵称" />
                </el-form-item>
                <el-form-item label="电话" prop="mobile">
                    <el-input v-model="formData.mobile" clearable placeholder="请填写电话" />
                </el-form-item>
                <el-form-item label="邮箱" prp="email">
                    <el-input v-model="formData.email" clearable placeholder="请填写邮箱" />
                </el-form-item>
                <el-form-item label="渠道来源" prop="comefrom">
                    <el-input v-model="formData.comefrom" clearable placeholder="请填写渠道来源" />
                </el-form-item>
                <el-form-item label="等级" prop="level_i">
                    <el-select class="flex-1" v-model="formData.level_id" clearable placeholder="请选择级别">
                        <el-option v-for="(item, index) in dictData.level" :key="index" :label="item.name"
                            :value="parseInt(item.value)" />
                    </el-select>
                </el-form-item>
                <el-form-item label="类型" :prop="popupTitle === '新增用户表' ? 'type' : ''">
                    <el-select v-model="formData.type" clearable placeholder="请选择类型">
                        <el-option v-for="(item, index) in dictData.type" :key="index" :label="item.name"
                            :value="parseInt(item.value)" />
                    </el-select>
                </el-form-item>
                <el-form-item label="时间" prop="update_time">
                    <el-date-picker v-model="formData.update_time" type="datetime" clearable placeholdr="请选择时间"
                        format="YYYY-MM-DD HH:mm:ss" />
                </el-form-item>
                <el-form-item label="启用" prop="status">
                    <el-switch v-model="formData.status" :active-value="1" :inactive-value="0" />
                </el-form-item>
                <el-form-item label="数据权限" prop="is_change">
                    <el-switch v-model="formData.is_change" :active-value="1" :inactive-value="0" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="memberEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiMemberRolesLists } from '@/api/member_roles'
import { apiMemberAdd, apiMemberEdit, apiMemberDetail } from '@/api/member'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑用户表' : '新增用户表'
})
//获取角色名称
const roles = ref<any[]>([])
const queryParams = reactive({
    role_name: "",
    identify: "",
    remark: "",
    sort: "",
})
const getRolesList = async () => {
    const res = await apiMemberRolesLists(queryParams)
    roles.value = res.lists
}
getRolesList()

// 表单数据
const formData = reactive({
    id: '',
    account: '',
    password: '',
    role_id: '',
    nickname: '',
    mobile: '',
    email: '',
    level_id: '',
    comefrom: '',
    update_time: '',
    ip: '',
    address: '',
    ac_num: '',
    remark: '',
    status: '',
    type: '',
    source: '',
    is_change: ''
})

// 表单验证
const formRules = reactive<any>({
    account: [
        {
            required: true,
            message: '请填写账号',
            trigger: ['blur']
        }
    ],
    password: [
        {
            required: true,
            message: '请填写密码',
            trigger: ['blur']
        }
    ],
    role_id: [
        {
            required: true,
            message: '请选择角色id',
            trigger: ['change', 'blur']
        }
    ],
    nickname: [
        {
            required: true,
            message: '请填写昵称',
            trigger: ['blur']
        }
    ],
    level_id: [
        {
            required: true,
            message: '请选择级别',
            trigger: ['change', 'blur']
        }
    ],
    ac_num: [
        {
            required: true,
            message: '请填写获得数量',
            trigger: ['blur']
        }
    ],
    type: [
        {
            required: true,
            message: '请选择类型',
            trigger: ['change', 'blur']
        }
    ]
})

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiMemberDetail({
        id: row.id
    })
    setFormData(data)
}

// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit' ? await apiMemberEdit(data) : await apiMemberAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}
// getRolesList()

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
